package com.aidex.quartz.util;

import java.time.*;

public class DateUtils {

    // 定义周末（不计入工作时间）
    private static final DayOfWeek WEEKEND_START = DayOfWeek.SATURDAY;
    private static final DayOfWeek WEEKEND_END = DayOfWeek.SUNDAY;
    // 定义工作日的工作时间（9:00-18:00）
    private static LocalTime WORKDAY_START = LocalTime.of(9, 0);
    private static LocalTime WORKDAY_END = LocalTime.of(18, 0);

    /**
     * 判断在工作时间段内，某个时间是否超过某小时
     *
     * @param endTime
     * @param hours
     * @param workStartTime
     * @param workEndTime
     * @return
     */
    public static boolean isExceededHours(LocalDateTime endTime, Integer hours, LocalTime workStartTime, LocalTime workEndTime) {

        if (null != workStartTime) {
            WORKDAY_START = workStartTime;
        }
        if (null != workEndTime) {
            WORKDAY_END = workEndTime;
        }

        LocalDateTime now = LocalDateTime.now();
        Duration totalWorkingDuration = calculateWorkingDuration(endTime, now, WORKDAY_START, WORKDAY_END);
        return totalWorkingDuration.compareTo(Duration.ofHours(hours)) > 0;
    }

    private static Duration calculateWorkingDuration(LocalDateTime start, LocalDateTime end, LocalTime WORKDAY_START, LocalTime WORKDAY_END) {
        if (start.isAfter(end)) {
            return Duration.ZERO;
        }

        Duration totalDuration = Duration.ZERO;
        LocalDateTime current = start;

        while (current.isBefore(end)) {
            // 如果是周末，跳过整天
            if (isWeekend(current.toLocalDate())) {
                current = current.plusDays(1).with(LocalTime.MIN);
                continue;
            }

            // 获取当天的有效工作时间段
            LocalTime currentTime = current.toLocalTime();
            LocalDateTime workStart = current.with(WORKDAY_START);
            LocalDateTime workEnd = current.with(WORKDAY_END);

            // 如果当前时间在工作时间之前，从工作开始时间计算
            if (currentTime.isBefore(WORKDAY_START)) {
                current = workStart;
            }
            // 如果当前时间在工作时间之后，跳到下一天
            else if (currentTime.isAfter(WORKDAY_END) || currentTime.equals(WORKDAY_END)) {
                current = current.plusDays(1).with(LocalTime.MIN);
            }
            // 当前时间在工作时间内
            else {
                // 计算到当天工作结束或end时间的最小值
                LocalDateTime endOfWorkingPeriod = workEnd.isBefore(end) ? workEnd : end;
                Duration dailyDuration = Duration.between(current, endOfWorkingPeriod);
                totalDuration = totalDuration.plus(dailyDuration);
                current = endOfWorkingPeriod.equals(workEnd)
                        ? current.plusDays(1).with(LocalTime.MIN)
                        : endOfWorkingPeriod;
            }
        }

        return totalDuration;
    }

    private static boolean isWeekend(LocalDate date) {
        DayOfWeek dayOfWeek = date.getDayOfWeek();
        return dayOfWeek == WEEKEND_START || dayOfWeek == WEEKEND_END;
    }

    public static void main(String[] args) {
        Integer hours = 6;
        // 测试示例
        LocalDateTime testTime = LocalDateTime.now().minusHours(8); // 8小时前
        boolean exceeded = isExceededHours(testTime, hours, null, null);
        System.out.println("是否超过6小时: " + exceeded);

        // 考虑非工作时间的测试
        LocalDateTime yesterdayEvening = LocalDateTime.now()
                .minusDays(1)
                .withHour(17)
                .withMinute(0); // 昨天17:00
        exceeded = isExceededHours(yesterdayEvening, hours, null, null);
        System.out.println("昨天17:00到现在是否超过" + hours + "小时: " + exceeded);
    }
}